﻿Imports System.IO

Public Class Frm_sahib_maktab
    Public avo As Avocat_bureau
    Dim bol As Boolean = True
    Dim loc As String = "photo.jpg"

    Private Sub Btn_parc_hay2a_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_hay2a.Click
        Me.Enabled = False
        Frm_Avoc_Bure_hay2a.txt_hay2a.Select()
        Frm_Avoc_Bure_hay2a.Show()
    End Sub

    Private Sub Btn_parc_ville_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_ville.Click
        Me.Enabled = False
        Frm_Avoc_Bure_ville.txt_ville.Select()
        Frm_Avoc_Bure_ville.Show()
    End Sub

    Private Sub Btn_ajou_tele_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele.Click
        Partager.typePhone = "Tel"

        '------
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_hatif.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_ajou_tele_bur_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele_bur.Click
        Partager.typePhone = "Fix"

        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_fix.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Fix" Then
                        Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        Me.Enabled = False
        Frm_Rech_sahib_maktab.Show()
    End Sub

    Private Sub Frm_sahib_maktab_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt_3a2ili.Select()
        Try
            Partager.moi.remplir_List_avocat_bureau()
            List_tous.Items.Clear()
            For Each khib As Avocat_bureau In Partager.moi.List_avocat_bureau
                If khib.type = "صاحب المكتب" Then
                    List_tous.Items.Add(New ListViewItem(New String() {khib.Id, khib.Nom_a, khib.Prenom_a, khib.Autorite, khib.Ville, khib.Adress, khib.Email}))
                End If
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Partager.com_ville(comb_ville, "Comb_Ville", "Ville")
        Partager.com_ville(Comb_hay2a, "Comb_hay2a", "hay2a")
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            Partager.close()
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة محام جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
                If txt_mail.Text <> "" Then
                    bol = Partager.regEmail(txt_mail.Text)
                    If (bol = False) Then
                        txt_mail.Text = "البريد الإلكتروني خاطئ"
                        txt_mail.ForeColor = Color.Red
                        txt_mail.TextAlign = HorizontalAlignment.Right
                    End If
                End If

                If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "" And bol = True) Then
                    If MsgBox("هل تريد فعلا إضافة هذا المحامي ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة صاحب المكتب") = MsgBoxResult.Yes Then

                        Try
                            Dim avovat_bur As New Avocat_bureau(1, "", txt_nom.Text, txt_prenom.Text, txt_3a2ili.Text, txt_chakhssi.Text, Now, Now, comb_ville.Text, "", 1, "", Comb_hay2a.Text, 1, txt_mail.Text, txt_adresse.Text, Partager.image(Pic_avoc), "", True, "صاحب المكتب", Check_majliss.Checked)
                            Partager.moi.Ajouter_avocat_bureau(avovat_bur)
                            'l'objet en cours
                            Dim lastItem As Integer = Partager.moi.List_avocat_bureau.Count - 1
                            avovat_bur = CType(Partager.moi.List_avocat_bureau.Item(lastItem), Avocat_bureau)

                            For Each d As Phone In Partager.list_hatif
                                avovat_bur.Ajouter_phone(d)
                            Next

                            For Each d As Phone In Partager.list_fix
                                avovat_bur.Ajouter_phone(d)
                            Next

                            List_tous.Items.Clear()
                            For Each khib As Avocat_bureau In Partager.moi.List_avocat_bureau
                                If khib.type = "صاحب المكتب" Then
                                    List_tous.Items.Add(New ListViewItem(New String() {khib.Id, khib.Nom_a, khib.Prenom_a, khib.Autorite, khib.Ville, khib.Adress, khib.Email}))
                                End If

                            Next
                            MsgBox("تمت إضافة المحامي بنجاح", MsgBoxStyle.Information, "نجاح")
                            Partager.vider(Me)
                        Catch ex As Exception

                            MsgBox(ex.Message)

                        End Try
                    End If

                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
            End If
        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        If List_tous.SelectedIndices.Count > 0 Then


            If txt_mail.Text <> "" Then
                bol = Partager.regEmail(txt_mail.Text)
                If (bol = False) Then
                    txt_mail.Text = "البريد الإلكتروني خاطئ"
                    txt_mail.ForeColor = Color.Red
                    txt_mail.TextAlign = HorizontalAlignment.Right
                End If
            End If

            If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "" And bol = True) Then

                If MsgBox("هل تريد فعلا تعديل معلومات هذا المحامي ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل صاحب المكتب") = MsgBoxResult.Yes Then
                    Try
                        Partager.close()
                        Dim avovat_bur As New Avocat_bureau(1, "", txt_nom.Text, txt_prenom.Text, txt_3a2ili.Text, txt_chakhssi.Text, Now, Now, comb_ville.Text, "", 1, "", Comb_hay2a.Text, 1, txt_mail.Text, txt_adresse.Text, Partager.image(Pic_avoc), "", True, "صاحب المكتب", Check_majliss.Checked)
                        Partager.moi.modifier_avocat_bureau(avo.Id, avovat_bur)
                        avovat_bur.Id = avo.Id
                        If Partager.list_hatif.Count > 0 Then
                            avovat_bur.suprimmer_phone(avo.Id, "Tel")
                            For Each d As Phone In Partager.list_hatif
                                avovat_bur.Ajouter_phone(d)
                            Next
                        End If
                        If Partager.list_fix.Count > 0 Then
                            avovat_bur.suprimmer_phone(avo.Id, "Fix")
                            For Each d As Phone In Partager.list_fix
                                avovat_bur.Ajouter_phone(d)
                            Next
                        End If
                        List_tous.Items.Clear()
                        For Each khib As Avocat_bureau In Partager.moi.List_avocat_bureau
                            If khib.type = "صاحب المكتب" Then
                                List_tous.Items.Add(New ListViewItem(New String() {khib.Id, khib.Nom_a, khib.Prenom_a, khib.Autorite, khib.Ville, khib.Adress, khib.Email}))
                            End If
                        Next
                        MsgBox("عملية التعديل تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me)
                    Catch ex As Exception

                        MsgBox(ex.Message)

                    End Try
                End If

            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Else
            MsgBox("اختر محام من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click

        Partager.close()
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا المحامي ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف صاحب المكتب") = MsgBoxResult.Yes Then
                Try
                    Partager.moi.suprimmer_avocat_bureau(avo.Id)
                    List_tous.Items.Clear()
                    For Each khib As Avocat_bureau In Partager.moi.List_avocat_bureau
                        If khib.type = "صاحب المكتب" Then
                            List_tous.Items.Add(New ListViewItem(New String() {khib.Id, khib.Nom_a, khib.Prenom_a, khib.Autorite, khib.Ville, khib.Adress, khib.Email}))
                        End If
                    Next
                    Partager.vider(Me)
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر محام من القائمة", MsgBoxStyle.Information, "حذف")
        End If
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged

        If List_tous.SelectedItems.Count > 0 Then
            Try
                Partager.close()
                Dim i As Integer = 0
                Dim id As Integer = CType(List_tous.SelectedItems(0).Text, Integer)

                Dim j As Integer = 0
                While i < List_tous.SelectedIndices.Count
                    avo = Partager.moi.List_avocat_bureau(j)
                    If avo.Id = id Then
                        i += 1
                    End If
                    j += 1
                End While

                txt_3a2ili.Text = avo.Nom_a
                txt_chakhssi.Text = avo.Prenom_a
                txt_nom.Text = avo.Nom
                txt_prenom.Text = avo.Prenom
                comb_ville.Text = avo.Ville
                Comb_hay2a.Text = avo.Autorite
                txt_adresse.Text = avo.Adress
                txt_mail.Text = avo.Email
                Check_majliss.Checked = avo.majlissA3la
                Dim bytBLOBData() As Byte = avo.Photo
                Dim stmBLOBData As New MemoryStream(bytBLOBData)
                Pic_avoc.Image = Image.FromStream(stmBLOBData)
                avo.remplir_List_list_phone()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Partager.list_hatif.Add(numhone)
                    ElseIf numhone.Type = "Fix" Then
                        Partager.list_fix.Add(numhone)
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

        If List_tous.SelectedIndices.Count <= 0 Then
            Partager.vider(Me)
            Check_majliss.Checked = False
        End If
    End Sub

   
    Private Sub Btn_parc_photo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_photo.Click
        With OpenF
            .FileName = ""
            .Title = "Ovrir la Photo"
            .RestoreDirectory = True
            .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            .Filter = "JPEG Files (*.JPG)|*.JPG"
            .FilterIndex = 1
        End With
        If OpenF.ShowDialog = Windows.Forms.DialogResult.OK Then
            Pic_avoc.Image = Image.FromFile(OpenF.FileName)
            Loc = OpenF.FileName
        End If
    End Sub

    Private Sub Btn_photo_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_photo_supp.Click
        Pic_avoc.Image = My.Resources.photo
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        txt_3a2ili.Select()
        Check_majliss.Checked = False
    End Sub

    Private Sub txt_mail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_mail.Click
        If bol = False Then
            txt_mail.Text = ""
        End If
        bol = True
    End Sub

    Private Sub txt_mail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_mail.TextChanged
        txt_mail.ForeColor = Color.Black
        txt_mail.TextAlign = HorizontalAlignment.Left
    End Sub
End Class